//
//  HotViewController.m
//  FAKER
//
//  Created by Jianwei on 15/11/2.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "HotViewController.h"
#import "BaseClassTableView.h"
#import "FitHeader.h"
#import "BaseClassImageView.h"
#import "AllHeader.h"
#import "WFAboutUsViewController.h"
#import "WFWeiboViewController.h"
#define OnTintColor_NIGHT [UIColor colorWithRed:0.33 green:0.67 blue:0.93 alpha:1]
#define OnTintColor_DAY [UIColor colorWithRed:0.97 green:0.29 blue:0.27 alpha:1]

@interface HotViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic, strong)BaseClassTableView *tableView;
@property(nonatomic, strong)BaseClassImageView *headerImageView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)UILabel *cachesLabel;
@property(nonatomic, strong)UISwitch *nightSwitch;
@property(nonatomic, strong)NSString *cachesPath;

@end

@implementation HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的";
    
    //创建顶部图片
    [self createHeaderImageView];
    
    [self createViewsOnCell];
}

- (void)loadView
{
    [super loadView];
    _dataSourceArray = [NSMutableArray arrayWithObjects:@"   清除缓存", @"   夜间模式", @"   关于我们", nil];
    
    //获取缓存路径
    _cachesPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library"] stringByAppendingPathComponent:@"Caches"];
//    NSLog(@"%@", _cachesPath);
    
    _tableView = [[BaseClassTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 210 * FITHEIGHT)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_tableView];
}

#pragma mark 创建cell上的部件
- (void)createViewsOnCell
{
    _nightSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 15 - 51, (40 - 31) / 2, 0, 0)];
    _nightSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"night"];
    [_nightSwitch addTarget:self action:@selector(clickNightSwitchAction) forControlEvents:UIControlEventValueChanged];
    
    _cachesLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 15 - 80, (40 - 30) / 2, 80, 30)];
//    _cachesLabel.textColor = [UIColor lightGrayColor];
    _cachesLabel.textAlignment = NSTextAlignmentRight;
    _cachesLabel.font = [UIFont systemFontOfSize:14];
    
    [self changeColor];
}

#pragma mark - 创建顶部图片
- (void)createHeaderImageView
{
    _headerImageView = [[BaseClassImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 210 * FITHEIGHT)];
//    _headerImageView.image = [UIImage imageNamed:@"PlaceHolder"];
//    [self.view addSubview:_headerImageView];
    [_tableView addSubview:_headerImageView];
}

#pragma mark - tableView
#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section) {
        return 1;
    }
    return _dataSourceArray.count - 1;
}
#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"user";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _dataSourceArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.5];
    [self setCellStyleWithCell:cell cellForRowAtIndexPath:indexPath];
    if (NSUSERDEFAULTS_NIGHT) {
        cell.backgroundColor = BGCOLOR_LIGHTBLACK;
        cell.textLabel.textColor = [UIColor lightGrayColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor darkGrayColor];
    }
    return cell;
}
#pragma mark 判断cell样式
- (void)setCellStyleWithCell:(UITableViewCell *)cell
       cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    switch (row) {
//        case 0:
//            [cell addSubview:_cachesLabel];
//            break;
//        case 1:
//            [cell addSubview:_nightSwitch];
//            break;
//            
//        case 2:
//        default:
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//            break;
//    }
    if (indexPath.section) {
        cell.textLabel.text = _dataSourceArray[2];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        if (indexPath.row) {
            [cell addSubview:_nightSwitch];
        } else {
            [cell addSubview:_cachesLabel];
        }
    }
}
#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    switch (indexPath.row) {
//        case 0:
//        {
//            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleAlert];
//            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//                NSLog(@"确定");
//                [ClearCaches clearCaches];
//                _cachesLabel.text = [NSString stringWithFormat:@"%@", [ClearCaches folderSizeAtPath:_cachesPath]];
//            }];
//            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//            [alertController addAction:confirmAction];
//            [alertController addAction:cancelAction];
//            [self presentViewController:alertController animated:YES completion:nil];
//        }
//            break;
//        case 1:
//            break;
////        case 2:
////            NSLog(@"意见反馈");
////            break;
//            
//        default:
////            NSLog(@"关于我们");
//        {
//            WFAboutUsViewController *wc = [[WFAboutUsViewController alloc]init];
//            [wc setHidesBottomBarWhenPushed:1];
//            [self.navigationController pushViewController:wc animated:1];
//        }
//            break;
//    }
    
    if (indexPath.section) {
        WFAboutUsViewController *wc = [[WFAboutUsViewController alloc]init];
        [wc setHidesBottomBarWhenPushed:1];
        [self.navigationController pushViewController:wc animated:1];
    } else {
        if (indexPath.row == 0) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"确定");
                [ClearCaches clearCaches];
                _cachesLabel.text = [NSString stringWithFormat:@"%@", [ClearCaches folderSizeAtPath:_cachesPath]];
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:confirmAction];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    return headerView;
}

#pragma mark - 点击switch方法
- (void)clickNightSwitchAction
{
//    NSLog(@"%d", _nightSwitch.on);
    if (_nightSwitch.on) {
        _nightSwitch.onTintColor = OnTintColor_NIGHT;
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"night"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
    }
    else {
        _nightSwitch.onTintColor = OnTintColor_DAY;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"night"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
    }
    [_tableView reloadData];
    [self changeColor];
}

#pragma mark - 滚动中(方法顶部图片)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= 0) {
        _headerImageView.frame = CGRectMake(0, scrollView.contentOffset.y, SCREEN_WIDTH, 210 * FITHEIGHT - scrollView.contentOffset.y);
    }
//    else {
//        _headerImageView.frame = CGRectMake(0, -scrollView.contentOffset.y, SCREEN_WIDTH, 210 * FITHEIGHT);
//    }
}

#pragma mark - 视图已经出现刷新缓存数据
- (void)viewDidAppear:(BOOL)animated
{
    _cachesLabel.text = [NSString stringWithFormat:@"%@", [ClearCaches folderSizeAtPath:_cachesPath]];
}

#pragma mark - 改变颜色
- (void)changeColor
{
    if (NSUSERDEFAULTS_NIGHT) {
        _nightSwitch.onTintColor = OnTintColor_NIGHT;
        _headerImageView.image = [UIImage imageNamed:@"Faker_night"];
        _cachesLabel.textColor = [UIColor lightGrayColor];
    } else {
        _nightSwitch.onTintColor = OnTintColor_DAY;
        _headerImageView.image = [UIImage imageNamed:@"Faker"];
        _cachesLabel.textColor = [UIColor grayColor];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
